package com.kang.until;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletResponse;

/**
 * 文件工具类
 * 
 * @author zs614
 *
 */
public class FileUtil {

	/**
	 * 项目根目录
	 */
	public static String ROOTPATH = "";
	/**
	 * 供应商附件保存路径
	 */
	public static String SUPPLIER_ANNEX_PATH = "";
	/**
	 * 供货协议保存路径
	 */
	public static String SUPPLY_AGREEMENT_ANNEX_PATH = "";
	/**
	 * 物品图片保存路径
	 */
	public static String ITEM_IMAGE_PATH = "";
	/**
	 * 临时文件保存路径
	 */
	public static String TEMP_PATH = "";
	/**
	 * 初始化文件保存路径
	 */
	static {
		File f = new File(FileUtil.class.getResource("/").getPath());
		f = f.getParentFile().getParentFile().getParentFile();
		ROOTPATH = f.getAbsolutePath() + "/wzgl_resource";
		try {
			ROOTPATH = URLDecoder.decode(ROOTPATH, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		SUPPLIER_ANNEX_PATH = ROOTPATH + "/supplier_annex/";
		SUPPLY_AGREEMENT_ANNEX_PATH = ROOTPATH + "/supply_agreement_annex/";
		ITEM_IMAGE_PATH = ROOTPATH + "/item_image/";
		TEMP_PATH = ROOTPATH + "/temp/";
		File file1 = new File(SUPPLIER_ANNEX_PATH);
		File file2 = new File(SUPPLY_AGREEMENT_ANNEX_PATH);
		File file3 = new File(ITEM_IMAGE_PATH);
		File file4 = new File(TEMP_PATH);
		file1.mkdirs();
		file2.mkdirs();
		file3.mkdirs();
		file4.mkdirs();
	}

	/**
	 * 下载文件
	 * 
	 * @param response
	 * @param filePath
	 *            文件路径
	 * @param fileName
	 *            文件名
	 */
	public static void download(HttpServletResponse response, String filePath, String fileName) {
		File file = new File(filePath);
		download(response, file, fileName);
	}

	/**
	 * 下载文件
	 * 
	 * @param response
	 * @param file
	 *            文件
	 * @param fileName
	 *            文件名
	 */
	public static void download(HttpServletResponse response, File file, String fileName) {

		if (fileName == null)
			fileName = file.getName();
		InputStream inputStream;
		try {
			inputStream = new BufferedInputStream(new FileInputStream(file));
			byte[] buffer = new byte[inputStream.available()];
			inputStream.read(buffer);
			inputStream.close();
			response.reset();
			// 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,这个文件名称用于浏览器的下载框中自动显示的文件名
			response.addHeader("Content-Disposition",
					"attachment;filename=" + new String(fileName.replaceAll(" ", "").getBytes("utf-8"), "iso8859-1"));
			response.addHeader("Content-Length", "" + file.length());
			OutputStream os = new BufferedOutputStream(response.getOutputStream());
			response.setContentType("application/octet-stream");
			os.write(buffer);// 输出文件
			os.flush();
			os.close();
		} catch (Exception e) {
			// e.printStackTrace();
			try {
				response.getOutputStream().write("<html><body><h2>该文件不存在</h2></body></html>".getBytes());
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
	}

	/**
	 * 删除文件
	 * 
	 * @param path
	 *            文件路径
	 */
	public static void delete(String path) {
		File file = new File(path);
		if (file.exists()) {
			file.delete();
		}
	}
}
